package com.richpencil.modules.service.impl;

import com.richpencil.modules.ki.domain.GenConfig;
import com.richpencil.modules.ki.domain.vo.ColumnInfo;
import com.richpencil.modules.ki.domain.vo.TableInfo;
import com.richpencil.exception.BadRequestException;
import com.richpencil.modules.utils.GenUtil;
import com.richpencil.modules.service.GeneratorService;
import com.richpencil.utils.PageUtil;
import com.richpencil.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @date 2019-01-02
 */
@Service
public class GeneratorOracleServiceImpl implements GeneratorService {
  
    //@PersistenceContext
    //@Qualifier("entityManagerFactoryK3")
    private EntityManager em;

    @Override
    public Object getTables(String name, int[] startEnd) {
        // 使用预编译防止sql注入
    	String oraclesql=" select table_name from all_tables where owner='waps' and table_name like ? ";
		/*
		 * String sql =
		 * "select table_name ,create_time , engine, table_collation, table_comment from information_schema.tables "
		 * + "where table_schema = (select database()) " +
		 * "and table_name like ? order by create_time desc";
		 */
        Query query = em.createNativeQuery(oraclesql);
        query.setFirstResult(startEnd[0]);
        query.setMaxResults(startEnd[1]-startEnd[0]);
        query.setParameter(1, StringUtils.isNotBlank(name) ? ("%" + name + "%") : "%%");
        List<Object[]> result = query.getResultList();
        List<TableInfo> tableInfos = new ArrayList<>();
        for (Object[] obj : result) {
            tableInfos.add(new TableInfo(obj[0],"","","", ""));
        }
        Query query1 = em.createNativeQuery("select count(*) from all_tables where owner='waps' ");
        Object totalElements = query1.getSingleResult();
        return PageUtil.toPage(tableInfos,totalElements);
    }

    @Override
    public Object getColumns(String name) {
        // 使用预编译防止sql注入
    	String oraclesql=" select COLUMN_NAME,NULLABLE,DATA_TYPE,'' as column_comment,'' as column_key, '' as extra from  FROM ALL_TAB_COLUMNS\r\n" + 
    			" WHERE TABLE_NAME =? " + 
    			" ORDER BY COLUMN_ID ";
    	
    	
        //String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
        //        "where table_name = ? and table_schema = (select database()) order by ordinal_position";
        Query query = em.createNativeQuery(oraclesql);
        query.setParameter(1, StringUtils.isNotBlank(name) ? name : null);
        List<Object[]> result = query.getResultList();
        List<ColumnInfo> columnInfos = new ArrayList<>();
        for (Object[] obj : result) {
            columnInfos.add(new ColumnInfo(obj[0],obj[1],obj[2],obj[3],obj[4],obj[5],null,"true"));
        }
        return PageUtil.toPage(columnInfos,columnInfos.size());
    }

    @Override
    public void generator(List<ColumnInfo> columnInfos, GenConfig genConfig, String tableName) {
        if(genConfig.getId() == null){
            throw new BadRequestException("请先配置生成器");
        }
        try {
            GenUtil.generatorCode(columnInfos,genConfig,tableName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }}
}
